﻿Imports SponsorshipLogic

Public Class ctlNotes

    Public Event SaveNote(ByRef oNote As Note)
    Public Event DeleteNote(ByRef oNote As Note, ByRef Cancel As Boolean)
    Private _notes As List(Of Note)
    Private _SelectedNote As Note
    Private _loading As Boolean

    Public Property Notes As List(Of Note)
        Get
            If IsNothing(_notes) Then
                _notes = New List(Of Note)
            End If
            Return _notes
        End Get
        Set(ByVal value As List(Of Note))
            _notes = value
            UpdateList()
        End Set
    End Property


    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        _SelectedNote = New Note
        With _SelectedNote
            .Title = "New Note Title"
            .Content = "New Note Content"
        End With

        RaiseEvent SaveNote(_SelectedNote)
        UpdateList()
        DisplayNote()
    End Sub


    Public Sub DisplayNote()
        _loading = True
        txtTitle.Text = _SelectedNote.Title
        txtContent.Text = _SelectedNote.Content
        lblNoteDetailContent.Text = _SelectedNote.Username & " " & _SelectedNote.Timestamp.ToString("dd/mm/yyyy")
        _loading = False
    End Sub

    Public Sub UpdateList()
        lstNotes.DisplayMember = "title"
        lstNotes.ValueMember = "id"
        lstNotes.DataSource = Notes
    End Sub

    Private Sub lstNotes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstNotes.SelectedIndexChanged
        If Not _SelectedNote Is Nothing Then
            If _SelectedNote.NeedsToSave Then
                If MessageBox.Show("Save changes?", "Note", MessageBoxButtons.YesNo) = vbYes Then
                    _SelectedNote.Save()
                End If
            End If
        End If

        _SelectedNote = Note.Load(lstNotes.SelectedValue)
        DisplayNote()

    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        RaiseEvent SaveNote(_SelectedNote)
    End Sub

    Private Sub txtTitle_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTitle.TextChanged
        If Not _loading Then
            _SelectedNote.Title = txtTitle.Text
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        _SelectedNote.AbandonChanges()
        DisplayNote()
    End Sub

    Private Sub txtContent_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtContent.TextChanged
        If Not _loading Then
            _SelectedNote.Content = txtContent.Text
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim bCancel As Boolean
        RaiseEvent DeleteNote(_SelectedNote, bCancel)
        If bCancel Then
            MessageBox.Show("Unable to delete note", "Note", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            MessageBox.Show("Note Deleted", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class
